<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 26, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class BasePagination {
	var $_totalPages;
	var $_totalItems;
	var $_recordPerPage;
	var $_link;
	
	var $_textFirst;
	var $_textPrev;
	var $_textNext;
	var $_textLast;
	
	/**
	 * Enter description here...
	 *
	 * @param Number - Total of Items $totalItems
	 * @param Number - Record per page $recordPerPage
	 * @param String - Link $link
	 */
	function __construct($totalItems, $recordPerPage = 10, $link = '?page') {
		$this->_totalItems = $totalItems;
		$this->_recordPerPage = $recordPerPage;
		$this->_link = $link;
		if ($this->_recordPerPage) {
			$this->_totalPages = ceil ( $this->_totalItems / $this->_recordPerPage );
		}
		else {
			$this->_totalPages = 0;
		}
		
		$this->_textFirst = 'First';
		$this->_textPrev = 'Prev';
		$this->_textNext = 'Next';
		$this->_textLast = 'Last';
	}
	
	/**
	 * Render Pager
	 *
	 * @param Page current $index
	 * @return HTML to show pager
	 */
	function renderPager($index) {
		$output = '';
		if ($this->_totalPages > 1) {
			$this->setPagerText ();
			$output .= '<div class="pager">';
			$output .= $this->getPagerFirst ( $index );
			$output .= $this->getPagerPrevious ( $index );
			$output .= $this->getPagerList ( $index );
			$output .= $this->getPagerNext ( $index );
			$output .= $this->getPagerLast ( $index );
			$output .= '</div>';
		}
		return $output;
	}
	
	/**
	 * Set Text for pager Link
	 *
	 * @param String $textFirst
	 * @param String $textPrev
	 * @param String $textNext
	 * @param String $textLast
	 */
	
	function setPagerText($textFirst = '', $textPrev = '', $textNext = '', $textLast = '') {
		if (! empty ( $textFirst )) {
			$this->_textFirst = $textFirst;
		}
		if (! empty ( $textPrev )) {
			$this->_textPrev = $textPrev;
		}
		if (! empty ( $textNext )) {
			$this->_textNext = $textNext;
		}
		if (! empty ( $textLast )) {
			$this->_textLast = $textLast;
		}
	}
	
	/**
	 * Get Link for 'First'
	 *
	 * @param current page $index
	 * @return URL
	 */
	function getPagerFirst($index) {
		$output = '';
		if ($index > 1) {
			$output = $this->getPagerTextLink ( $this->_textFirst, 1 );
		}
		return $output;
	}
	
	/**
	 * Get Link for 'Previous'
	 *
	 * @param current page $index
	 * @return URL
	 */
	function getPagerPrevious($index) {
		$output = '';
		if ($index > 1) {
			$output = $this->getPagerTextLink ( $this->_textPrev, $index - 1 );
		}
		return $output;
	}
	
	/**
	 * Get Link for 'Next'
	 *
	 * @param current page $index
	 * @return URL
	 */
	function getPagerNext($index) {
		$output = '';
		if ($index < $this->_totalPages) {
			$output = $this->getPagerTextLink ( $this->_textNext, $index + 1 );
		}
		return $output;
	}
	
	/**
	 * Get Link for 'Last'
	 *
	 * @param current page $index
	 * @return URL
	 */
	function getPagerLast($index) {
		$output = '';
		if ($index < $this->_totalPages) {
			$output = $this->getPagerTextLink ( $this->_textLast, $this->_totalPages );
		}
		return $output;
	}
	
	/**
	 * Get
	 *
	 * @param current page $index
	 * @param Quantity of List $quantity
	 * @return unknown
	 */
	function getPagerList($index, $quantity = 7) {
		$output = '<span class="pager-list">';
		// Calculate various markers within this pager piece:
		// Middle is used to "center" pages around the current page.
		$pager_middle = ceil ( $quantity / 2 );
		// current is the page we are currently paged to
		$pager_current = $index;
		// first is the first page listed by this pager piece (re quantity)
		$pager_first = $pager_current - $pager_middle + 1;
		// last is the last page listed by this pager piece (re quantity)
		$pager_last = $pager_current + $quantity - $pager_middle;
		// max is the maximum page number
		$pager_max = $this->_totalPages;
		// End of marker calculations.
		

		// Prepare for generation loop.
		$i = $pager_first;
		if ($pager_last > $pager_max) {
			// Adjust "center" if at end of query.
			$i = $i + ($pager_max - $pager_last);
			$pager_last = $pager_max;
		}
		if ($i <= 0) {
			// Adjust "center" if at start of query.
			$pager_last = $pager_last + (1 - $i);
			$i = 1;
		}
		// End of generation loop preparation.
		

		// When there is more than one page, create the pager list.
		if ($i != $pager_max) {
			if ($i > 1) {
				$output .= $this->getExtPage ( '...' );
			}
			
			// Now generate the actual pager piece.
			for(; $i <= $pager_last && $i <= $pager_max; $i ++) {
				if ($i < $pager_current) {
					//$output .= theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters);
					$output .= $this->getPagerNumberLink ( $i, $i );
				}
				if ($i == $pager_current) {
					$output .= $this->getActivePage ( $i );
				}
				if ($i > $pager_current) {
					//$output .= theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters);
					$output .= $this->getPagerNumberLink ( $i, $i );
				}
			}
			
			if ($i <= $pager_max) {
				$output .= $this->getExtPage ( '...' );
			}
		}
		$output .= '</span>';
		
		return $output;
	}
	
	/**
	 * Get Pager Link
	 *
	 * @param String $text
	 * @param Number - current page $index
	 * @return unknown
	 */
	function getPagerNumberLink($text, $index) {
		$link = $this->_link . '=' . $index; 
//		$link = $this->_link . ':' . $index; //binh -> feelthere
		$output = '<a href="' . $link . '">  ' . $text . '  </a>';
		return $output;
	}
	
	/**
	 * Get Pager Text Link
	 *
	 * @param String $text
	 * @param Number - current page $index
	 * @return unknown
	 */
	function getPagerTextLink($text, $index) {
//		$link = $this->_link . '=' . $index;
		$link = $this->_link . ':' . $index; //binh -> feelthere
		$output = '<a href="' . $link . '">  ' . $text . '  </a>';
		return $output;
	}
	
	/**
	 * Get the link of pager
	 *
	 * @param int $index
	 * @return string
	 */
	function getPagerLink($index) {
		if ($index < 1 || $index > $this->_totalPages) {
			return "#";
		}
//		return $this->_link . '=' . $index;	
		return $this->_link . ':' . $index;	//binh -> feelthere
	}
	
	/**
	 * Get Active Page
	 *
	 * @param String $text
	 * @return unknown
	 */
	function getActivePage($text) {
		return '<strong>' . $text . '</strong>';
	}
	
	/**
	 * Get Extention Page
	 *
	 * @param String $text
	 * @return unknown
	 */
	function getExtPage($text = '...') {
		return $text;
	}
}

?>